<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      <!--      <el-input v-model="dataForm.sort" placeholder="排序值"></el-input>-->
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-switch v-model="dataForm.status" active-value="1" inactive-value="0"
                 active-color="#13ce66" inactive-color="#ff4949">
      </el-switch>
<!--      <el-input v-model="dataForm.status" placeholder="是否开启 0否 1是"></el-input>-->
    </el-form-item>
<!--    <el-form-item label="创建时间" prop="createTime">-->
<!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="更新时间" prop="updateTime">-->
<!--      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="是否删除 0未删除 1已删除" prop="deleted">-->
<!--      <el-input v-model="dataForm.deleted" placeholder="是否删除 0未删除 1已删除"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        //控制当前弹框是否显示
        visible: false,
        //表单提交数据
        dataForm: {
          id: 0,
          name: '',
          sort: 0,
          status: '1',

        },
        //约束
        dataRule: {
          name: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          // sort: [
          //   { required: true, message: '排序值不能为空', trigger: 'blur' }
          // ],
          // status: [
          //   { required: true, message: '状态', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      //页面初始化时执行
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/forum/communitycategory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.communityCategory.name
                this.dataForm.sort = data.communityCategory.sort
                this.dataForm.status = data.communityCategory.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          console.log(this.dataForm)
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/forum/communitycategory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
